/*
 * This file is a part of the SWFer project.
 *
 * Copyright (C) 2012 Michael Bradshaw <mjbshaw@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include "MainWindow.hpp"
#include "ui_MainWindow.h"

#include "SwfView.hpp"
#include "SimpleSwfView.hpp"

#include <QLayout>
#include <QFileDialog>
#include <QDockWidget>

#include <swfer/Swf.hpp>
#include <swfer/SimpleSwf.hpp>

MainWindow::MainWindow(QWidget* parent) :
    SwfMainDisplay(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
	
    connect(ui->actionNew, SIGNAL(triggered()), this, SLOT(newSwf()));
    connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(openSwf()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::newSwf()
{
	if (ui->actionAdvanced->isChecked())
	{
		// TODO: this is leaking memory...
		swfer::Swf* swf = new swfer::Swf;

		// TODO: who's responsible for the memory?

		addDisplay(new SwfView(swf, this));
	}
	else
	{
		// TODO: this is leaking memory...
		swfer::SimpleSwf* swf = new swfer::SimpleSwf;

		// TODO: who's responsible for the memory?

		addDisplay(new SimpleSwfView(swf, this));
	}
}

void MainWindow::openSwf()
{
    QString path = QFileDialog::getOpenFileName(this, "Open SWF...", QString(), "*.swf");
	if (!path.isEmpty())
	{
		if (ui->actionAdvanced->isChecked())
		{
			// TODO: this is leaking memory...
			swfer::Swf* swf = new swfer::Swf(path.toStdString());

			// TODO: who's responsible for the memory?

			addDisplay(new SwfView(swf, this));
		}
		else
		{
			// TODO: this is leaking memory...
			swfer::SimpleSwf* swf = new swfer::SimpleSwf(path.toStdString());

			// TODO: who's responsible for the memory?

			addDisplay(new SimpleSwfView(swf, this));
		}
	}
}

void MainWindow::addDisplay(QWidget* widget)
{
    QDockWidget* dock = new QDockWidget(this);
    dock->setWidget(widget);
    dock->setWindowTitle(widget->windowTitle());

    addDockWidget(Qt::LeftDockWidgetArea, dock);

    widget->setVisible(true);
}
